{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5436020b",
   "metadata": {},
   "source": [
    "# Access intermediate steps\n",
    "\n",
    "In order to get more visibility into what an agent is doing, we can also return intermediate steps. This comes in the form of an extra key in the return value, which is a list of (action, observation) tuples."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a26be808",
   "metadata": {},
   "outputs": [],
   "source": [
    "# pip install wikipedia"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b2b0d119",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain import hub\n",
    "from langchain.agents import AgentExecutor, create_openai_functions_agent\n",
    "from langchain_community.chat_models import ChatOpenAI\n",
    "from langchain_community.tools import WikipediaQueryRun\n",
    "from langchain_community.utilities import WikipediaAPIWrapper\n",
    "\n",
    "api_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=100)\n",
    "tool = WikipediaQueryRun(api_wrapper=api_wrapper)\n",
    "tools = [tool]\n",
    "\n",
    "# Get the prompt to use - you can modify this!\n",
    "# If you want to see the prompt in full, you can at: https://smith.langchain.com/hub/hwchase17/openai-functions-agent\n",
    "prompt = hub.pull(\"hwchase17/openai-functions-agent\")\n",
    "\n",
    "llm = ChatOpenAI(temperature=0)\n",
    "\n",
    "agent = create_openai_functions_agent(llm, tools, prompt)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d329c3d",
   "metadata": {},
   "source": [
    "Initialize the AgentExecutor with `return_intermediate_steps=True`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6abf3b08",
   "metadata": {},
   "outputs": [],
   "source": [
    "agent_executor = AgentExecutor(\n",
    "    agent=agent, tools=tools, verbose=True, return_intermediate_steps=True\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "837211e8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m\n",
      "Invoking: `Wikipedia` with `Leo DiCaprio`\n",
      "\n",
      "\n",
      "\u001b[0m\u001b[36;1m\u001b[1;3mPage: Leonardo DiCaprio\n",
      "Summary: Leonardo Wilhelm DiCaprio (; Italian: [diˈkaːprjo]; born November 1\u001b[0m\u001b[32;1m\u001b[1;3mLeonardo DiCaprio's middle name is Wilhelm.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "response = agent_executor.invoke({\"input\": \"What is Leo DiCaprio's middle name?\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e1a39a23",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(AgentActionMessageLog(tool='Wikipedia', tool_input='Leo DiCaprio', log='\\nInvoking: `Wikipedia` with `Leo DiCaprio`\\n\\n\\n', message_log=[AIMessage(content='', additional_kwargs={'function_call': {'name': 'Wikipedia', 'arguments': '{\\n  \"__arg1\": \"Leo DiCaprio\"\\n}'}})]), 'Page: Leonardo DiCaprio\\nSummary: Leonardo Wilhelm DiCaprio (; Italian: [diˈkaːprjo]; born November 1')]\n"
     ]
    }
   ],
   "source": [
    "# The actual return type is a NamedTuple for the agent action, and then an observation\n",
    "print(response[\"intermediate_steps\"])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.1"
  },
  "vscode": {
   "interpreter": {
    "hash": "b1677b440931f40d89ef8be7bf03acb108ce003de0ac9b18e8d43753ea2e7103"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
